package com.cremamobile.filemanager.file;

import java.util.HashMap;

import com.cremamobile.filemanager.R;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;

public class IconMap {
	public static final int TYPE_NOT_SET = -1;
	public static final int FILE_TYPE_TEXT = 1;
	public static final int FILE_TYPE_IMAGE = 2;
	public static final int FILE_TYPE_AUDIO = 3;
	public static final int FILE_TYPE_MOV = 4;
	public static final int FILE_TYPE_ZIP = 5;
	public static final int FILE_TYPE_DISK = 6;
	public static final int FILE_TYPE_APK = 7;
	
	public static final int FILE_TYPE_DIR = 8;
	public static final int FILE_TYPE_NAVIGATION_UP = 9;
	
	public static final int FILE_TYPE_NONE = 0;

	private static IconMap mThis;
	
	private HashMap<Integer, Bitmap> iconMap;
	private HashMap<String, Integer> iconCodeMap;
	
	private IconMap() {}
	
	public static IconMap createIconMap(Context context) {
		if (mThis == null || mThis.iconMap == null || mThis.iconCodeMap == null) {
			mThis = null;
			mThis = new IconMap();
			
			HashMap<String, Integer> map = new HashMap<String, Integer>();
			
			Integer text = Integer.valueOf(FILE_TYPE_TEXT);
			map.put("txt", text);
			map.put("doc", text);
			map.put("docx", text);
			map.put("docm", text);
			map.put("rtf", text);
			map.put("hwp", text);
			map.put("hwt", text);
			map.put("html", text);
			map.put("htm", text);
			map.put("xml", text);
			map.put("ppt", text);
			map.put("pptx", text);
			map.put("xls", text);
			map.put("xlsx", text);
			map.put("pdf", text);
	
			Integer img = Integer.valueOf(FILE_TYPE_IMAGE);
			map.put("jpg", img);
			map.put("jpeg", img);
			map.put("png", img);
			map.put("gif", img);
			map.put("bmp", img);
			map.put("ico", img);
			map.put("psd", img);
			map.put("tif", img);
			map.put("tiff", img);
	
			Integer aud = Integer.valueOf(FILE_TYPE_AUDIO);
			map.put("mp3", aud);
			map.put("ogg", aud);
			map.put("wma", aud);
			map.put("flac", aud);
			map.put("mid", aud);
			map.put("midi", aud);
			map.put("ac3", aud);
			map.put("aac", aud);
			map.put("ra", aud);
	
			Integer mov = Integer.valueOf(FILE_TYPE_MOV);
			map.put("rm", mov);
			map.put("ram", mov);
			map.put("avi", mov);
			map.put("mpg", mov);
			map.put("mpeg", mov);
			map.put("asf", mov);
			map.put("asx", mov);
			map.put("wmv", mov);
			map.put("mov", mov);
			map.put("mp4", mov);
			map.put("swf", mov);
			map.put("flv", mov);
			map.put("mkv", mov);
	
			Integer zip = Integer.valueOf(FILE_TYPE_ZIP);
			map.put("zip", zip);
			map.put("alz", zip);
			map.put("egg", zip);
			map.put("rar", zip);
			map.put("7z", zip);
			map.put("cab", zip);
			map.put("tar", zip);
			map.put("ace", zip);
			map.put("arj", zip);
			map.put("tgz", zip);
	
			Integer iso = Integer.valueOf(FILE_TYPE_DISK);
			map.put("iso", iso);
			map.put("lcd", iso);
			map.put("img", iso);
	
			Integer apk = Integer.valueOf(FILE_TYPE_APK);
			map.put("apk", apk);
	
			Integer dir = Integer.valueOf(FILE_TYPE_DIR);
			map.put("dir", dir);
			
			Integer navdir = Integer.valueOf(FILE_TYPE_NAVIGATION_UP);
			map.put("navdir", navdir);
			
			mThis.iconCodeMap = map;
	
			HashMap<Integer, Bitmap> iMap = new HashMap<Integer, Bitmap>();
			
			BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.icon_blue);
			Bitmap textIcon = drawable.getBitmap();
	
			iMap.put(text, textIcon);
	
			drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.icon_green);
			Bitmap imgIcon = drawable.getBitmap();
	
			iMap.put(img, imgIcon);
	
			drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.icon_purple);
			Bitmap audIcon = drawable.getBitmap();
	
			iMap.put(aud, audIcon);
	
			drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.icon_orange);
			Bitmap movIcon = drawable.getBitmap();
	
			iMap.put(mov, movIcon);
	
			drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.icon_yellow);
			Bitmap zipIcon = drawable.getBitmap();
	
			iMap.put(zip, zipIcon);
	
			drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.icon_red);
			Bitmap isoIcon = drawable.getBitmap();
	
			iMap.put(iso, isoIcon);
	
			drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.icon_grey);
			Bitmap nonIcon = drawable.getBitmap();
	
			iMap.put(Integer.valueOf(FILE_TYPE_NONE), nonIcon);
			
			drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.big_list_folder_green);
			Bitmap dirIcon = drawable.getBitmap();
	
			iMap.put(Integer.valueOf(FILE_TYPE_DIR), dirIcon);
	
			drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.hightrank_folder_01);
			Bitmap navdirIcon = drawable.getBitmap();
	
			iMap.put(Integer.valueOf(FILE_TYPE_NAVIGATION_UP), navdirIcon);
			
			mThis.iconMap = iMap;
		}		
		return mThis;
	}
	
	public Bitmap getIcon(Context context, String ext, String fullPath) {
		if (ext == null || ext.length() <= 0)
			return iconMap.get(Integer.valueOf(FILE_TYPE_NONE));
			
		Integer type = iconCodeMap.get(ext);
		if (type == null) {
			return iconMap.get(Integer.valueOf(FILE_TYPE_NONE));
		}
		
		if (type == FILE_TYPE_APK) {
			return getPackageIcon(context, fullPath);
		} else {
			return iconMap.get(type);
		}
	}
	
	public int getIconType(String ext) {
		if (ext == null || ext.length() <= 0)
			return TYPE_NOT_SET;
		Integer type = iconCodeMap.get(ext);
		if (type == null)
			return TYPE_NOT_SET;
		else
			return type;
	}
	
	private Bitmap getPackageIcon(Context context, String fullPath) {
        PackageInfo packageInfo = context.getPackageManager().getPackageArchiveInfo(fullPath, PackageManager.GET_ACTIVITIES);
        if(packageInfo != null) {
            ApplicationInfo appInfo = packageInfo.applicationInfo;
            if (Build.VERSION.SDK_INT >= 8) {
                appInfo.sourceDir = fullPath;
                appInfo.publicSourceDir = fullPath;
            }
            Drawable icon = appInfo.loadIcon(context.getPackageManager());
            return ((BitmapDrawable) icon).getBitmap();
        }
        return null;
	}

}
